package es.mfp.practicos.view.util;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.springframework.context.ApplicationContext;

import es.mfp.practicos.domain.LineaTelefonica;
import es.mfp.practicos.service.LineaTelefonicaService;
import es.mfp.practicos.web.context.AppContext;

@FacesConverter(value = "lineaTelefonicaConverter")
public class LineaTelefonicaConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {

		ApplicationContext ctx = AppContext.getApplicationContext();
		LineaTelefonicaService lineaTelefonicaService = ctx
				.getBean(LineaTelefonicaService.class);
		LineaTelefonica lt = lineaTelefonicaService.buscarPorNumero(arg2);

		if (lt == null) {
			// ..
		}

		return lt;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		LineaTelefonica lt = (LineaTelefonica) arg2;
		return lt.getNumero();
	}

}
